package com.yike.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yike.common.exception.BusinessException;
import com.yike.common.tool.LoginUtil;
import com.yike.common.tool.StringUtil;
import com.yike.user.entity.Merchant;
import com.yike.user.entity.User;
import com.yike.user.mapper.MerchantMapper;
import com.yike.user.mapper.UserMapper;
import com.yike.user.param.AddMerchantParam;
import com.yike.user.param.SetMerchantParam;
import com.yike.user.param.UpdateMerchantParam;
import com.yike.user.service.MerchantService;
import com.yike.user.param.MerchantPageParam;
import com.yike.common.base.BaseServiceImpl;
import com.yike.common.pagination.Paging;
import com.yike.common.pagination.PageInfo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.user.vo.MerchantVo;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 商户表 服务实现类
 *
 * @author Sakura
 * @since 2024-09-03
 */
@Slf4j
@Service
public class MerchantServiceImpl extends BaseServiceImpl<MerchantMapper, Merchant> implements MerchantService {

    @Autowired
    private MerchantMapper merchantMapper;
    @Autowired
    private UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveMerchant(AddMerchantParam addMerchantParam) throws Exception {
        // 此处添加商户的时候会同步创建一个用户账号
        // 所以需要验证手机号是否存在用户，如果存在则走另外一个接口直接将用户设置为商户
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getMobile, addMerchantParam.getMobile()));
        if (user != null) {
            throw new BusinessException("当前手机号已存在账号，请直接设置为商户即可");
        }

        // 创建一个用户
        user = new User();
        user.setMobile(addMerchantParam.getMobile());
        user.setUserNo(StringUtil.random(32)); // 生成用户ID
        user.setNickname(addMerchantParam.getContact());
        user.setCreateBy(LoginUtil.getUserId());
        user.setStatus(1); // 状态为正常
        userMapper.insert(user);

        // 创建一条商户记录
        Merchant merchant = new Merchant();
        BeanUtils.copyProperties(addMerchantParam, merchant);
        merchant.setMerchantNo("86" + StringUtil.getStrNo());
        merchant.setUserId(user.getId());
        merchant.setCreateBy(LoginUtil.getUserId());
        return super.save(merchant);
    }

    @Override
    public boolean setMerchant(SetMerchantParam setMerchantParam) throws Exception {
        // 先获取用户信息
        User user = userMapper.selectOne(Wrappers.lambdaQuery(User.class)
                .eq(User::getUserNo, setMerchantParam.getUserNo()));
        if (user == null) {
            throw new BusinessException("用户信息异常");
        }
        // 判断当前用户是否已经是商户
        Merchant merchant = merchantMapper.selectOne(Wrappers.lambdaQuery(Merchant.class)
                .eq(Merchant::getUserId, user.getId()));
        if (merchant != null) {
            throw new BusinessException("当前用户已经是商户");
        }

        // 创建一条商户记录
        merchant = new Merchant();
        BeanUtils.copyProperties(setMerchantParam, merchant);
        merchant.setMerchantNo("86" + StringUtil.getStrNo());
        merchant.setUserId(user.getId());
        merchant.setCreateBy(LoginUtil.getUserId());
        return super.save(merchant);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateMerchant(UpdateMerchantParam updateMerchantParam) throws Exception {
        // 获取代理商信息
        Merchant merchant = merchantMapper.selectOne(Wrappers.lambdaQuery(Merchant.class)
                .eq(Merchant::getMerchantNo, updateMerchantParam.getMerchantNo()));
        if (merchant == null) {
            throw new BusinessException("商户信息异常");
        }
        BeanUtils.copyProperties(updateMerchantParam, merchant);
        merchant.setUpdateBy(LoginUtil.getUserId());
        merchant.setUpdateTime(new Date());
        return super.updateById(merchant);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteMerchant(String merchantNo) throws Exception {
        // 获取商户信息
        Merchant merchant = merchantMapper.selectOne(Wrappers.lambdaQuery(Merchant.class)
                .eq(Merchant::getMerchantNo, merchantNo));
        if (merchant == null) {
            throw new BusinessException("商户信息异常");
        }
        return super.removeById(merchant.getId());
    }

    @Override
    public MerchantVo getMerchant(String merchantNo) throws Exception {
        return merchantMapper.getMerchantVo(merchantNo);
    }

    @Override
    public Paging<MerchantVo> getMerchantPageList(MerchantPageParam merchantPageParam) throws Exception {
        Page<Merchant> page = new PageInfo<>(merchantPageParam);
        IPage<MerchantVo> iPage = merchantMapper.getMerchantList(page, merchantPageParam);
        return new Paging<MerchantVo>(iPage);
    }

}
